import axios from "axios";
import {getStorage} from "@/assets/js/localStorage";

let baseURL = "http://localhost:8089";
// 网关api
baseURL = "http://localhost:9091/api/fresheveryday";

const instance = axios.create({
    baseURL: baseURL
});

instance.interceptors.request.use(config => {
    let token = getStorage("token");

    if (token) {
        config.headers["Authorization"] = token;
    }

    return config;
}, error => {
    console.log(error);
    Promise.reject(error).then(r => console.log(r));
});

export const getData = (url, data, success, error) => {
    instance.get(url, {
        params: data
    }).then(function (response) {
        success(response);
    }).catch(function (err) {
        error(err);
    });
};

export const postForm = (url, data, success, error) => {
    instance.post(url, data).then(function (response) {
        success(response);
    }).catch(function (err) {
        error(err);
    });
};

export const error = function(res) {
    let result = res.response.data;
    let code = result.code;

    if (code === 401 || code === 403) {
        console.log(result.message);

        location.href = loginPage;
    }
};

/**
 * 登录页面URL地址
 * @type {string}
 */
export const loginPage = "/login";

/**
 * 系统首页URL地址
 * @type {string}
 */
export const indexPage = "/";